package com.lowrisk.springcloud;

import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;

/**
 * @Author jiadianduo
 * @Description: 监听服务实体创建的
 * @Date:Created in 2019/3/24 22:32
 */
@Configuration
public class ServiceInfoUtil implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {

    private static  EmbeddedServletContainerInitializedEvent event;

    @Override
    public void onApplicationEvent(EmbeddedServletContainerInitializedEvent embeddedServletContainerInitializedEvent) {
        event = embeddedServletContainerInitializedEvent;
    }

    /**
     * 获取当前实例的端口号
     * @return
     */
    public static int getPort(){
        return event.getEmbeddedServletContainer().getPort();
    }
}
